export default {
	namespaced: true,
	state() {
		return {
			// 初始一个状态数据
			address: uni.getStorageSync('address') || null,
			userProfile: uni.getStorageSync('userProfile') || {
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
				nickname: '微信用户'
			},
			token: uni.getStorageSync('token') || ''
		}
	},
	getters: {
		// 勾选地址
		fullCheck(state) {
			if (!state.address) return '';
			const {
				provinceName,
				cityName,
				countyName,
				detailInfo
			} = state.address
			return provinceName + cityName + countyName + detailInfo
		}
	},
	mutations: {
		getProfile(state, userProfile) {
			state.userProfile = userProfile
			uni.setStorageSync('userProfile', userProfile)
		},
		saveAddress(state, address) {
			// 将获取的地址记录在 vuex 中
			state.address = address;
			// 将获取的收货地址记录在本地存储
			uni.setStorageSync('address', address);
		},
		saveToken(state, token) {
			state.token = token
			uni.setStorageSync('token', token)
		}
	},
	actions: {
		async getToken() {
			const [err, {
				code
			}] = await uni.login();
			//登录凭证
			// console.log(code);
			const [err1, {
				encryptedData,
				signature,
				iv,
				rawData
			}] = await uni.getUserInfo();

			const {
				data: res
			} = await uni.$http.post('/api/public/v1/users/wxlogin', {
				code,
				encryptedData,
				rawData,
				iv,
				signature
			});
			if (res.message.token) this.commit('m_user/saveToken', res.message.token);
			console.log(res);

		}
	}
}
